package jfox.platform.organization.action;

import java.util.List;
import javax.ejb.EJB;

import jfox.platform.function.action.NodeAction.DeleteNodeInvocation;
import jfox.platform.infrastructure.SuperAction;
import jfox.platform.organization.bo.CompanyBO;
import jfox.platform.organization.entity.Company;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;

/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Service(id="company")
public class CompanyAction extends SuperAction{

	@EJB
	CompanyBO companyBO;
/**
 * 获取组织机构所有记录
 * @param actionContext
 * @throws Exception
 */	
	@ActionMethod(name = "organization", successView = "platform/organization/organization_management.vhtml")
	public void listNodeTree(ActionContext actionContext) throws Exception {
        List<Company> companys = companyBO.getAllCompanys();
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("companys", companys);
    }
/**
 * 新增公司
 * @param actionContext
 * @throws Exception
 */	
    @ActionMethod(name = "newview", successView = "platform/organization/new_company.vhtml")
    public void newCompanyView(ActionContext actionContext) throws Exception {
    	
    }
/**
 * 修改公司
 * @param actionContext
 * @throws Exception
 */    
    @ActionMethod(name = "editview", successView = "platform/organization/edit_company.vhtml", invocationClass = DeleteCompanyInvocation.class)
    public void editCompanyView(ActionContext actionContext) throws Exception {
        DeleteCompanyInvocation invocation = (DeleteCompanyInvocation)actionContext.getInvocation();
        Company company = companyBO.getCompanyById(invocation.getId());
        actionContext.getPageContext().setAttribute("company", company);
    }
/**
 * 保存新增公司
 * @param actionContext
 * @throws Exception
 */   
    @ActionMethod(name = "new", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml",invocationClass = NewCompanyInvocation.class)
    public synchronized void newCompany(ActionContext actionContext) throws Exception {
        NewCompanyInvocation invocation = (NewCompanyInvocation)actionContext.getInvocation();
        Company company = new Company();
        company.setName(invocation.getName());
        company.setParentId(invocation.getParentId());
        company.setPostAddress(invocation.getPostAddress());
        company.setPostCode(invocation.getPostCode());
        company.setLinkman(invocation.getLinkman());
        company.setEmail(invocation.getEmail());
        company.setMobile(invocation.getMobile());
        company.setPhone(invocation.getPhone());
        company.setFax(invocation.getFax());
        company.setRegion(invocation.getRegion());
        company.setFoundYear(invocation.getFoundYear());
        company.setScale(invocation.getScale());
        company.setVstatus(invocation.getVstatus());
        company.setPriority(invocation.getPriority());
        companyBO.newCompany(company);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, company);
    }
/**
 * 保存修改公司
 * @param actionContext
 * @throws Exception
 */   
    @ActionMethod(name = "edit", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml",invocationClass = EditCompanyInvocation.class)
    public synchronized void editCompany(ActionContext actionContext) throws Exception {
        EditCompanyInvocation invocation = (EditCompanyInvocation)actionContext.getInvocation();
        Company company = companyBO.getCompanyById(invocation.getId());
        company.setName(invocation.getName());
        company.setParentId(invocation.getParentId());
        company.setPostAddress(invocation.getPostAddress());
        company.setPostCode(invocation.getPostCode());
        company.setLinkman(invocation.getLinkman());
        company.setEmail(invocation.getEmail());
        company.setMobile(invocation.getMobile());
        company.setPhone(invocation.getPhone());
        company.setFax(invocation.getFax());
        company.setRegion(invocation.getRegion());
        company.setFoundYear(invocation.getFoundYear());
        company.setScale(invocation.getScale());
        company.setVstatus(invocation.getVstatus());
        company.setPriority(invocation.getPriority());
        companyBO.updateCompany(company);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, company);
    }
/**
 * 删除公司记录
 * @param actionContext
 * @throws Exception
 */    
    @ActionMethod(name = "delete", successView = "include/json_response.vhtml", invocationClass = DeleteNodeInvocation.class)
    public synchronized void deleteCompany(ActionContext actionContext) throws Exception {
        DeleteCompanyInvocation invocation = (DeleteCompanyInvocation)actionContext.getInvocation();
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, invocation.getId());
        companyBO.deleteCompany(invocation.getId());

    }

    public static class DeleteCompanyInvocation extends Invocation {

        private long id;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }
    }
   
    public static class EditCompanyInvocation extends NewCompanyInvocation{
    	
    	private long id;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}
    	
    }
    
    public static class NewCompanyInvocation extends Invocation{
    	
    	private String name;
    	private String postAddress;   
    	private String postCode;
    	private String linkman;
    	private String email;
    	private String mobile;
    	private String phone;	
    	private String fax; 	
    	private Integer region; 	
    	private Integer foundYear;	
    	private Integer scale;	
    	private long parentId;
    	private int vstatus;
    	private int priority;
    	
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPostAddress() {
			return postAddress;
		}

		public void setPostAddress(String postAddress) {
			this.postAddress = postAddress;
		}

		public String getPostCode() {
			return postCode;
		}

		public void setPostCode(String postCode) {
			this.postCode = postCode;
		}

		public String getLinkman() {
			return linkman;
		}

		public void setLinkman(String linkman) {
			this.linkman = linkman;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getMobile() {
			return mobile;
		}

		public void setMobile(String mobile) {
			this.mobile = mobile;
		}

		public String getPhone() {
			return phone;
		}

		public void setPhone(String phone) {
			this.phone = phone;
		}

		public String getFax() {
			return fax;
		}

		public void setFax(String fax) {
			this.fax = fax;
		}

		public Integer getRegion() {
			return region;
		}

		public void setRegion(Integer region) {
			this.region = region;
		}

		public Integer getFoundYear() {
			return foundYear;
		}

		public void setFoundYear(Integer foundYear) {
			this.foundYear = foundYear;
		}

		public Integer getScale() {
			return scale;
		}

		public void setScale(Integer scale) {
			this.scale = scale;
		}

		public long getParentId() {
			return parentId;
		}

		public void setParentId(long parentId) {
			this.parentId = parentId;
		}

		public int getVstatus() {
			return vstatus;
		}

		public void setVstatus(int vstatus) {
			this.vstatus = vstatus;
		}

		public int getPriority() {
			return priority;
		}

		public void setPriority(int priority) {
			this.priority = priority;
		}
    }
    
}
